package com.yiren.dbaa.module.practice_set.basestation.model;

import android.app.Activity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.basestation.bean.BaseStationBean;
import com.yiren.dbaa.module.practice_set.basestation.contract.CalibrationContract;
import com.yiren.dbaa.net.OkHttpUtils;
import com.yiren.dbaa.net.bean.TestBaseBean;
import com.yiren.dbaa.net.interf.StringCallback;

import org.apache.commons.lang.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lwb
 * @date : 2022/12/14 11:07
 * @description: 标定基站
 */
public class CalibrationModel implements CalibrationContract.Model {

    private Activity activity;
    private UserParamConfigBean userParamConfig;
    private Gson gson;

    public CalibrationModel(Activity activity) {
        this.activity = activity;
        userParamConfig = LoadConfigApi.loadUserParamConfig(activity);
        gson = new Gson();
    }

    @Override
    public void uploadBaseStation(BaseStationBean baseStationBean, CommonCallBack callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", baseStationBean.getId());
        map.put("name", "我的基站" + baseStationBean.getId());
        map.put("longitude", baseStationBean.getLongitude());
        map.put("latitude", baseStationBean.getLatitude());
        map.put("altitude", baseStationBean.getAltitude());
        map.put("address", baseStationBean.getAddress());
        map.put("distance", baseStationBean.getDistance());
        map.put("calibration", 1);
        map.put("userId", userParamConfig.getUserInfo().getId());
        map.put("createTm", new Date());
        new Thread(new Runnable() {
            @Override
            public void run() {
                String line = OkHttpUtils.getInstance().z(AddressConstant.uploadStationInfo().getUrl(), new Gson().toJson(map));
                if (StringUtils.isNotEmpty(line)) {
                    TestBaseBean<String> testBaseBean = gson.fromJson(line, new TypeToken<TestBaseBean<String>>() {
                    }.getType());
                    if (testBaseBean.isSuccess()) {
                        callBack.success(testBaseBean.getData());
                    } else {
                        callBack.fail("发布失败，请重试！");
                    }
                } else {
                    callBack.fail("上传基站信息失败，请重新标定！");
                }
            }
        }).start();
    }

    @Override
    public void updateBaseBoxId(String oldBaseId, String newBaseId, CommonCallBack callBack) {
        OkHttpUtils.getInstance().getStringByAsync(activity, AddressConstant.updateStationId("?oldBaseId=" + oldBaseId + "&newBaseId=" + newBaseId), new StringCallback() {
            @Override
            public void getString(String result) {
                TestBaseBean testBaseBean = gson.fromJson(result, new TypeToken<TestBaseBean>() {
                }.getType());
                if (testBaseBean.isSuccess()) {
                    callBack.success("");
                } else {
                    callBack.fail("更换基站盒失败，请重试！");
                }
            }
        });
    }

    @Override
    public void updateBaseBoxName(String baseId, String baseName, CommonCallBack callBack) {
        OkHttpUtils.getInstance().getStringByAsync(activity, AddressConstant.updateStationName("?name=" + baseName + "&id=" + baseId), new StringCallback() {
            @Override
            public void getString(String result) {
                TestBaseBean testBaseBean = gson.fromJson(result, new TypeToken<TestBaseBean>() {
                }.getType());
                if (testBaseBean.isSuccess()) {
                    callBack.success("");
                } else {
                    callBack.fail("更换名称失败，请重试！");
                }
            }
        });
    }
}
